// pages/nearby/nearby.js
import util from "../../utils/util";
import consts from "../../js/const";

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        that.setData({lat:res.latitude,lng:res.longitude},(res)=>{
          that.searchInstStations();
        });
      },
      fail(res){
        console.log(res);
      }
    })
  },
  searchInstStations(event){
    let that=this;
    let params={};
    params.lat=that.data.lat;
    params.lng=that.data.lng;
    if(event && event.detail){
      params.company=event.detail;
    }else{
      params.distance=10;
    }
    util.promise('/searchInspectionStations',params
    ).then(res=>{
      if(res.data){
        let markers=[];
        let companys={};
        res.data.forEach(function (row,index){
          if(index===0){
            that.data.lat=row.lat;
            that.data.lng=row.lng;
          }
          companys[index]=row.company;
          markers.push(
              {
                id: index,
                latitude: row.lat,
                longitude: row.lng,
                iconPath: consts.getImageUrl('location-1.png'),
                callout: {
                  content: row.company,
                  color: '#ff0000',
                  fontSize: 14,
                  borderWidth: 1,
                  borderRadius: 10,
                  borderColor: '#999999',
                  bgColor: '#fff',
                  padding: 5,
                  display: 'ALWAYS',
                  textAlign: 'center',
                  anchorY:5
                }
              });
        })
        that.data.markers=markers;
        that.data.companys=companys;
        that.setData(that.data);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  markertap(event) {
    let that = this;
    let data = that.data;
    wx.openLocation({
      latitude: data.lat,
      longitude: data.lng,
      name: data.companys[event.detail.markerId],
      scale: 16
    })
    console.log(event);

  },
  callouttap(event) {
    this.markertap(event);
  },
  labeltap(event){
    this.markertap(event);
  }
})